package com.treeroot.springbootwithelastic.com.treeroot.es.controller;


import com.treeroot.springbootwithelastic.com.treeroot.es.service.ProductService;
import org.elasticsearch.action.bulk.BulkResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @author treeroot
 */
@RestController
@RequestMapping("/")
@CrossOrigin
public class EsController {

    @Resource
    private ProductService productService;

    /**
     * 爬取京东数据并添加进es库里
     *
     * @param keyword 关键词
     * @return 返回
     * @throws Exception 异常
     */
    @GetMapping("/export/{keyword}")
    public BulkResponse export(@PathVariable String keyword) throws Exception {
        System.out.println("===========" + keyword);
        return productService.export(keyword);
    }

    /**
     * 搜索es库里数据
     *
     * @param keyword     关键词
     * @param currentPage 当前页
     * @param pageSize    每页多少个
     * @return 返回
     * @throws Exception 异常
     */
    @GetMapping("search/{currentPage}/{pageSize}/{keyword}")
    public Map<String,Object> search(@PathVariable String keyword, @PathVariable Integer currentPage, @PathVariable Integer pageSize) throws Exception {
        return productService.search(keyword, currentPage, pageSize);
    }

}
